#include "ZContainer.h"

namespace Zdib
{
	CContainer::CContainer(CContainer *parent)
	{
		m_Parent=parent;
		if(m_Parent)
		{
			m_Parent->AddChildren(this);
		}
		m_IDMax=0;
		SetPos(0,0);
		m_Show=true;
		m_CallBackList=new TZList(1);
		OldScreenX=-1;
		OldScreenY=-1;
		m_RefrashDrawPos=true;
		this->myEventReceiver=NULL;
		SetIsNowFocus(false);
	}

	CContainer::~CContainer()
	{
		int i;
		CContainer *temp;
		PTimeCallBack tempcb;
		if(m_IsNowFocus)
		{
			root->SetOnFocus(NULL);
		}
		if(m_Parent)
		{
			m_Parent->DelChildren(this);
		}
		m_Parent=NULL;
		for(i=m_Childrens.count-1;i>=0;i--)
		{
			temp=(CContainer *)m_Childrens.items(i);
			delete temp;
		}
		for(i=m_CallBackList->count-1;i>=0;i--)
		{
			tempcb=(PTimeCallBack)m_CallBackList->items(i);
			tempcb->IsOver=true;
		}
		delete m_CallBackList;
	}

	CContainer *CContainer::GetParent()
	{
		return m_Parent;
	}

	void CContainer::SetID(int id)
	{
		ID=id;
	}

	void CContainer::EvaluationZ(int z)
	{
		Z=z;
	}

	void CContainer::AddChildren(CContainer *chi)
	{
		if(m_IDMax==0)
		{
			m_Childrens.setlength(20);
		}
		
		chi->SetID(m_IDMax);
		chi->EvaluationZ(m_IDMax);
		m_Childrens.add((LPVOID)chi);
		m_IDMax+=1;
	}

	void CContainer::ChanegChidrenZ(CContainer *chi,int z)
	{
		int i;
		int pos=chi->GetZ();
		CContainer *temp;
		if(chi->GetZ()==z)
		{
			return;
		}
		if(z>=m_Childrens.count-1)
		{
			m_Childrens.del(pos);
			m_Childrens.add((LPVOID)chi);
			for(i=pos;i<m_Childrens.count;i++)
			{
				temp=(CContainer *)m_Childrens.items(i);
				temp->SetID(i);
				temp->EvaluationZ(i);
			}
		}
		else
		{
			if(pos>z)
			{
				m_Childrens.del(pos);
				m_Childrens.insert(z,(LPVOID)chi);
				for(i=z;i<m_Childrens.count;i++)
				{
					temp=(CContainer *)m_Childrens.items(i);
					temp->SetID(i);
					temp->EvaluationZ(i);
				}
			}
			else
			{
				m_Childrens.del(pos);
				m_Childrens.insert(z-1,(LPVOID)chi);
				for(i=pos;i<m_Childrens.count;i++)
				{
					temp=(CContainer *)m_Childrens.items(i);
					temp->SetID(i);
					temp->EvaluationZ(i);
				}
			}
		}
	}

	void CContainer::DelChildren(CContainer *chi)
	{
		int i,pos;
		CContainer *temp;
		pos=chi->ID;
		m_Childrens.del(chi->ID);
		m_IDMax-=1;
		for(i=pos;i<m_Childrens.count;i++)
		{
			temp=(CContainer *)m_Childrens.items(i);
			temp->SetID(i);
			temp->EvaluationZ(i);
		}
	}

	void CContainer::SetParent(CContainer *parent)
	{
		if(m_Parent)
		{
			m_Parent->DelChildren(this);
		}
		m_Parent=parent;
		if(m_Parent)
		{
			m_Parent->AddChildren(this);
		}
	}

	void CContainer::SetZ(int z)
	{
		m_Parent->ChanegChidrenZ(this,z);
	}

	int CContainer::GetID()
	{
		return ID;
	}

	int CContainer::GetZ()
	{
		return Z;
	}
	void CContainer::SetRoot(CContainer *lp)
	{
		root=lp;
	}
	TPos CContainer::GetPos()
	{
		TPos pos;
		pos.x=X;
		pos.y=Y;
		return pos;
	}

	TPos CContainer::GetScreenPos()
	{
		if(m_Parent)
		{
			TPos pos=m_Parent->GetScreenPos();
			pos.x+=X;
			pos.y+=Y;
			return pos;
		}
		else
			return GetPos();
	}

	TPos CContainer::ScreenPos()
	{
		TPos pos;
		pos.x=ScreenX;
		pos.y=ScreenY;
		return pos;
	}

	PTimeCallBack CContainer::SetCallBack(LPVOID po,LPVOID (*CallBack)(LPVOID lp,LPVOID argv[]),unsigned short int frame,LPVOID argv[])
	{
		PTimeCallBack t;
		t=root->SetCallBack(po,CallBack,frame,argv);
		m_CallBackList->add((LPVOID)t);
		return t;
	}

	void CContainer::FreeCallBack(PTimeCallBack t)
	{
		int i;
		PTimeCallBack temp;
		if(t==NULL)
			return;
		t->IsOver=true;
		for(i=0;i<=m_CallBackList->count-1;i++)
		{
			temp=(PTimeCallBack)m_CallBackList->items(i);
			if(t==temp)
			{
				m_CallBackList->del(i);
				break;
			}
		}
	}

	void CContainer::SetEventReceiver(Zdib::CEventReceiver * myevent)
	{
		this->myEventReceiver=myevent;
	}
}